<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <img src="@/assets/images/logo.svg" alt="Logo" class="logo">
        <h1 class="title">学生管理系统</h1>
      </div>
      
      <el-form 
        ref="loginForm" 
        :model="loginForm"
        :rules="loginRules"
        class="login-form" 
        label-position="top"
      >
        <el-form-item prop="username" label="用户名">
          <el-input 
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            :clearable="true"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="password" label="密码">
          <el-input 
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            :type="showPassword ? 'text' : 'password'"
            :clearable="true"
            @keyup.enter="handleLogin"
          >
            <template #suffix>
              <el-icon class="cursor-pointer" @click="showPassword = !showPassword">
                <Hide v-if="showPassword" />
                <View v-else />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© {{ new Date().getFullYear() }} Howlaisi SMS. All Rights Reserved.</p>
      </div>
    </div>
  </div>
</template>

<script>
/**
 * 用户登录页面
 */
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    const loginForm = ref(null)
    
    // 登录表单数据
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    // 表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
      ]
    }
    
    // 是否显示密码明文
    const showPassword = ref(false)
    // 加载状态
    const loading = ref(false)
    
    // 处理登录
    const handleLogin = () => {
      loginForm.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            await userStore.loginAction(loginForm)
            await userStore.getUserInfoAction()
            
            ElMessage.success('登录成功')
            
            // 重定向到首页或指定页面
            const redirect = route.query.redirect || '/'
            router.push(redirect)
          } catch (error) {
            console.error('登录失败:', error)
            ElMessage.error('登录失败，请检查用户名和密码')
          } finally {
            loading.value = false
          }
        } else {
          return false
        }
      })
    }
    
    return {
      loginForm,
      loginRules,
      showPassword,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.login-content {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
}

.title {
  margin-top: 15px;
  font-size: 24px;
  color: #303133;
  font-weight: 600;
}

.login-form {
  margin-bottom: 30px;
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.login-footer {
  text-align: center;
  color: #909399;
  font-size: 12px;
}

.cursor-pointer {
  cursor: pointer;
}
</style> 